﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aribaccio;
using Aribaccio.Copyright;
using Aribaccio.Core;
using Aribaccio.Logger;
using BDPAutodetect.Mgrcore;

namespace BDPAutodetect.Client
{
    /// <summary>
    /// 应用程序加载器
    /// </summary>
    public class AppLoader
    {
        /// <summary>
        /// 注册版权信息实例
        /// </summary>
        protected IRegisterCopyMsg? RegisterCopyright { get; set; }
        /// <summary>
        /// 注册版权期限实例
        /// </summary>
        protected IRegisterCopyLimits RegisterCopyLimits { get; set; }
        /// <summary>
        /// 结果回调事件
        /// </summary>
        public Action<string>? ResultEvent { get; set; }
        /// <summary>
        /// 数据管理集合
        /// </summary>
        public List<IManager> Managers { get; protected set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public AppLoader(IRegisterCopyLimits argRegisterCopyLimits)
        {
            this.RegisterCopyLimits = argRegisterCopyLimits;
            this.Managers = new List<IManager>();
        }

        /// <summary>
        /// 初始化加载数据与服务
        /// </summary>
        public async Task InitializeService()
        { 
            //从容器中获取实例对象
            this.CallbackResultEvent("数据服务 => 初始化中...");

            //角色权限管理初始化
            await this.InitializeManager<RolePowerManager>();
            //用户管理初始化
            await this.InitializeManager<UserManager>();
            //从机设备管理初始化
            await this.InitializeManager<SlaveManager>();
            //工位管理初始化
            await this.InitializeManager<StationManager>();
            //指标及指标协议初始化
            await this.InitializeManager<TargetSlaveManager>();
            //参数管理初始化
            await this.InitializeManager<ParameterManager>();

            //设备存储及管理初始化
            await this.InitializeManager<StorageDeviceManager>();
            //检测记录管理初始化
            await this.InitializeManager<DetectRecordManager>();
            //检测设备管理初始化
            await this.InitializeManager<DetectDeviceManager>();
            //检测任务管理初始化
            await this.InitializeManager<DetectJobManager>();

            this.CallbackResultEvent("数据服务 => 初始化完成");
        }

        /// <summary>
        /// 初始化数据管理服务
        /// </summary>
        /// <returns></returns>
        protected async Task InitializeManager<T>() where T : BaseManager
        {
            var manager = ContainerManager.Instance.GetContainerServiceRequired<T>();
            if (manager == null) return;
            this.CallbackResultEvent($"{manager.Name} => 初始化中...");
            var result = manager.Initialize();
            await result.ConfigureAwait(false);

            this.Managers.Add(manager);          
        }

        /// <summary>
        /// 获取版权期限
        /// </summary>
        public IRegisterCopyMsg GetCopyrightLimits()
        {
            if (this.RegisterCopyright != null) return this.RegisterCopyright;
            this.RegisterCopyright = this.RegisterCopyLimits.CheckRegisterCopyLimits();
            return this.RegisterCopyright;
        }


        /// <summary>
        /// 退出
        /// </summary>
        public void Logout()
        {

        }

        /// <summary>
        /// 返回事件信息
        /// </summary>
        protected void CallbackResultEvent(string argResultMsg)
        {
            ResultEvent?.Invoke(argResultMsg);
        }
    }
}
